﻿#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent),MaxNo(0)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tableWidget->setStyleSheet("selection-background-color:rgb(23,244,123)");
    init();

    lineEdit=new QLineEdit;
    pushBtn=new QPushButton;
    label=new QLabel;

    statusBar()->addWidget(lineEdit);
    statusBar()->addWidget(pushBtn);
    statusBar()->addWidget(label);

    lineEdit->setMaxLength(200);
    pushBtn->setText("查找");
    connect(pushBtn,SIGNAL(clicked()),this,SLOT(onButtonFind()));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::init()
{
    ui->tableWidget->clear();
    QStringList headList={"姓名","性别","出生日期","职位","是否已婚"};
    ui->tableWidget->setColumnCount(headList.size());

    for(int i=0;i<ui->tableWidget->columnCount();i++)
    {
        QTableWidgetItem*item=new QTableWidgetItem(headList[i]);
        ui->tableWidget->setHorizontalHeaderItem(i,item);
        QFont font=item->font();
        font.setPointSize(12);
        font.setBold(true);
        font.setFamily("微软雅黑");
        item->setFont(font);
        item->setForeground(Qt::red);
    }
    QHeaderView *headView=ui->tableWidget->horizontalHeader();
    headView->setSectionResizeMode(QHeaderView::Stretch);

    headView->setSectionResizeMode(1,QHeaderView::Fixed);
    ui->tableWidget->setColumnWidth(1,100);


    int rows=100;
    ui->tableWidget->setRowCount(rows);

    QStringList jobs={"开发尚","高级工程师","架构师","程序员","牛马"};
    for(int i=0;i<rows;i++)
    {
        MaxNo++;
        creatRows(i,QString::asprintf("员工%d",MaxNo),MaxNo,rand()%2?"男":"女",QDate(rand()%10+2000,rand()%12+1,rand()%30+1),jobs[rand()%5],rand()%2);
    }


}

void MainWindow::creatRows(int rowNumber, QString name, int number, QString gender, QDate birthday, QString job, bool married)
{
    QTableWidgetItem*item=new QTableWidgetItem(name);
    item->setData(Qt::UserRole,QVariant(number));
    item->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
    ui->tableWidget->setItem(rowNumber,0,item);

    item=new QTableWidgetItem(gender);
    QIcon icon;
    if(gender=="男")
    {
        icon.addFile(":/image/boy.png");
    }
    else
    {
        icon.addFile(":/image/girl.png");
    }
    item->setIcon(icon);
    item->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
    ui->tableWidget->setItem(rowNumber,1,item);

    item=new QTableWidgetItem(birthday.toString("yyyy-MM-dd"));
    item->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
    ui->tableWidget->setItem(rowNumber,2,item);

    item=new QTableWidgetItem(job);
    item->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
    ui->tableWidget->setItem(rowNumber,3,item);

    item=new QTableWidgetItem(married?"已婚":"未婚");
    if(married)
    {
        item->setBackground(Qt::gray);
    }
    item->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
    ui->tableWidget->setItem(rowNumber,4,item);

}


void MainWindow::on_action_exit_triggered(bool checked)
{
    if(checked)
    {
        ui->tableWidget->setEditTriggers(QAbstractItemView::DoubleClicked|QAbstractItemView::SelectedClicked);

    }
    else
    {
        ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
    }
}

void MainWindow::on_action_insert_triggered()
{
    int currentRow=ui->tableWidget->currentRow();
    MaxNo++;
    ui->tableWidget->insertRow(currentRow);
    creatRows(currentRow,"未知",MaxNo,"男",QDate(2000,1,1),"未知",false);
    ui->tableWidget->selectRow(currentRow);
    on_action_exit_triggered(true);
    insertRow=currentRow;
}

void MainWindow::on_tableWidget_itemSelectionChanged()
{
    if(ui->action_exit->isChecked())return;
    if(insertRow==ui->tableWidget->currentRow())
    {
        on_action_exit_triggered(true);
    }
    else
    {
        on_action_exit_triggered(false);
    }
}

void MainWindow::on_action_delete_triggered()
{
    auto item=ui->tableWidget->currentRow();
    if(ui->tableWidget->currentItem()->isSelected())
    {
        ui->tableWidget->removeRow(item);
    }


}

void MainWindow::onButtonFind()
{
    ui->tableWidget->clearSelection();//清除选中
    QString ret=lineEdit->text().trimmed();
    if(ret.length()>0)
    {
        auto items=ui->tableWidget->findItems(ret,Qt::MatchContains);
        for(auto it=items.begin();it!=items.end();it++)
        {
            (*it)->setSelected(true);
        }
    }

}

void MainWindow::on_tableWidget_cellClicked(int row, int column)
{
    QString ret;
    for(int i=0;i<ui->tableWidget->columnCount();i++)
    {
        ret+=ui->tableWidget->item(row,i)->text()+"/";
    }
    label->setText(ret);
}
